#region usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

using CUtil;

#endregion

namespace CStock
{
	
	/// <summary>
	/// Summary description for Portfolios.
	/// </summary>
	public class ListQuotes : List<Quote> , IListQuotes
	{
	
		private object parent;

        #region Constructor

		public ListQuotes() : base()
		{
		}
		//the parameter constructor is needed because I read the data of the share 
		//on demand (when the this.Count = 0
        public ListQuotes(object Myparent)
            : base()
		{
			parent = Myparent;
		}

		#endregion

		#region ArrayList Override


        public void Add(IQuote q)
        {
            base.Add(q as Quote);
        }
		public int IndexOf(DateTime dt)
		{
		
			
            if (Count == 0)
                return -1;
            int i = -1;
			IQuoteBase qold = this[Count -1];
			//quick check from where to begin
			if(dt < this[0].DATE)
			{
				return -1;
			}
			else
			{
				for(i = this.Count -1; i > -1; i--)
				{
					if(this[i].DATE <= dt)
					{
						return i;

					}
				}
			}
			return i;
			
			
		}
		#endregion

		#region Implementation
		
		/// <summary>
		/// 
		/// </summary>
		//[System.Xml.Serialization.XmlIgnoreAttribute()]
		public IQuoteBase this[DateTime dt]
		{
			get
			{
				//1.get quotes if the array is empty
				if(this.Count < 1)
				{
					//Debug.Assert(false, "No get quotes in this implementation ");

					Debug.Assert(parent != null,"Parent of quote is null");
					if(this.parent == null)
					{
						return null;
					}
					
					GetQuotes();
				}

				if(this.Count < 1)
				{
					Debug.Assert(false,"Could not get quote array for " + this.parent.ToString());
					return null;
				}
					

				//2. Get the item starting at the end
				IQuoteBase qold ;//= this[Count -1];
				for(int i = this.Count -1; i > -1; i--)
				{
					qold = this[i];
					//IQuoteBase qw;				
					if(this[i].DATE <= dt)
					{
						return qold;

					}
					
				}
				return this[0];

			}
			set
			{
				if(this.Count < 1)
					return ;

				for(int i = this.Count -1; i > -1; i--)
				{			
					if(this[i].DATE == dt)
					{
						this[i] = value as Quote;

					}
				}
				
			}
		}

		public void GetQuotes()
		{
			
			IDataClass dataClass = parent as IDataClass ;
			if(dataClass != null)
			{
				object o = dataClass;
				if(dataClass.DeSerialize(ref o))
				{
					IListQuotes qc = dataClass.QuoteCollection;
					for(int i = 0; i < qc.Count; i++)
					{
						this.Add(dataClass.QuoteCollection[i]);
					}
				}
			}

		}
      
		#endregion

	
	}
	
}
